// DataTable.cpp: implementation of the CDataTable class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "a1.h"
#include "DataTable.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDataTable::CDataTable()
{
	m_iPerPageRecord = 20;
}
CDataTable::~CDataTable()
{
}
void CDataTable::SelectListCtrl(CListCtrl *list)
{
this->m_list=list;
}
bool CDataTable::RefreshListView(CString sql)
{
	m_list->DeleteAllItems();
	int n=this->m_pstrlistFields_Names[0].GetCount();
	m_iColumnCount = n;
	if(!m_runsql.CheckSQLResult(sql))
	{
		for(int i=0; i<n; i++ )
		{
			m_list->SetColumnWidth( i, LVSCW_AUTOSIZE_USEHEADER);
		}
		return false;
	}
	m_list->SetRedraw(false);	
	_variant_t value,field;
	int i=0;
	POSITION pos=m_pstrlistFields_Names[0].GetHeadPosition();
	int iTotal = GetRecordCount();
	m_iTotalPage = iTotal / m_iPerPageRecord + 1;  
    m_iCurrentPage = 1;
	for (int i=0; (i< m_iPerPageRecord) &&(!m_runsql.m_recordset->adoEOF);i++)
	{
		for(int j=0;j<n;j++)
		{
		field=this->m_pstrlistFields_Names[0].GetNext(pos);
		value=m_runsql.m_recordset->GetCollect(field);
		if(value.vt!=VT_NULL)
			if(j==0)
			m_list->InsertItem(i,(char*)(_bstr_t)value);
			else
			m_list->SetItemText(i,j,(char*)(_bstr_t)value);
		}
		m_runsql.m_recordset->MoveNext();
		pos=m_pstrlistFields_Names[0].GetHeadPosition();
	}
	for( i=0; i<n; i++ )
	{
			m_list->SetColumnWidth( i, LVSCW_AUTOSIZE);
			m_list->SetColumnWidth( i, LVSCW_AUTOSIZE_USEHEADER);	
	}
	m_list->SetSelectionMark(0);
	m_list->SetRedraw(true);
	m_list->Invalidate();

	return true;
}
CString CDataTable::GetNameFromField(CString field)
{	
	POSITION pos=NULL;int i;
	for(pos=this->m_pstrlistFields_Names[0].GetHeadPosition(),i=0;pos!=NULL;i++)
	{
		if(field==this->m_pstrlistFields_Names[0].GetNext(pos))
		{
			return this->m_pstrlistFields_Names[1].GetAt(this->m_pstrlistFields_Names[1].FindIndex(i));
		}
	}
	return CString("");
}
CString CDataTable::GetFieldFromName(CString name)
{	
	POSITION pos=NULL;int i;
	for(pos=this->m_pstrlistFields_Names[1].GetHeadPosition(),i=0;pos!=NULL;i++)
	{
		if(name==this->m_pstrlistFields_Names[1].GetNext(pos))
		{
			return this->m_pstrlistFields_Names[0].GetAt(this->m_pstrlistFields_Names[0].FindIndex(i));
		}
	}
	return CString("");
}

void CDataTable::nextPage()
{
	m_iCurrentPage++;
	if (m_iCurrentPage > m_iTotalPage)
	{
		m_iCurrentPage--;
		return;
	}
	m_runsql.m_recordset->MoveFirst();
	int iCount = (m_iCurrentPage - 1) * m_iPerPageRecord;
	for (int i=0; (i<iCount)&&(!m_runsql.m_recordset->adoEOF);i++)
	{
        m_runsql.m_recordset->MoveNext();
	}
	if (m_runsql.m_recordset->adoEOF)
	{
        return;
	}
	m_list->DeleteAllItems();
	_variant_t value,field;
	POSITION pos=m_pstrlistFields_Names[0].GetHeadPosition();
	for (int i=0; (i< m_iPerPageRecord) &&(!m_runsql.m_recordset->adoEOF);i++)
	{
		for(int j=0;j<m_iColumnCount;j++)
		{
			field=this->m_pstrlistFields_Names[0].GetNext(pos);
			value=m_runsql.m_recordset->GetCollect(field);
			if(value.vt!=VT_NULL)
			{	
				if(j==0)
				{
					m_list->InsertItem(i,(char*)(_bstr_t)value);
				}
				else
				{
					m_list->SetItemText(i,j,(char*)(_bstr_t)value);
				}
			}
			
		}
		m_runsql.m_recordset->MoveNext();
		pos=m_pstrlistFields_Names[0].GetHeadPosition();
		m_list->SetColumnWidth( i, LVSCW_AUTOSIZE);
		m_list->SetColumnWidth( i, LVSCW_AUTOSIZE_USEHEADER);
	}
	m_list->SetSelectionMark(0);
	m_list->SetRedraw(true);
	m_list->Invalidate();
}

void CDataTable::prePage()
{
    m_iCurrentPage--;
	if (m_iCurrentPage < 1)
	{
		m_iCurrentPage++;
		return;
	}
	m_runsql.m_recordset->MoveFirst();
	int iCount = (m_iCurrentPage - 1) * m_iPerPageRecord;
	for (int i=0; (i<iCount)&&(!m_runsql.m_recordset->adoEOF);i++)
	{
        m_runsql.m_recordset->MoveNext();
	}
	if (m_runsql.m_recordset->adoEOF)
	{
        return;
	}
	m_list->DeleteAllItems();
	_variant_t value,field;
	POSITION pos=m_pstrlistFields_Names[0].GetHeadPosition();
	for (int i=0; (i< m_iPerPageRecord) &&(!m_runsql.m_recordset->adoEOF);i++)
	{
		for(int j=0;j<m_iColumnCount;j++)
		{
			field=this->m_pstrlistFields_Names[0].GetNext(pos);
			value=m_runsql.m_recordset->GetCollect(field);
			if(value.vt!=VT_NULL)
			{	
				if(j==0)
				{
					m_list->InsertItem(i,(char*)(_bstr_t)value);
				}
				else
				{
					m_list->SetItemText(i,j,(char*)(_bstr_t)value);
				}
			}
			
		}
		m_runsql.m_recordset->MoveNext();
		pos=m_pstrlistFields_Names[0].GetHeadPosition();
		m_list->SetColumnWidth( i, LVSCW_AUTOSIZE);
		m_list->SetColumnWidth( i, LVSCW_AUTOSIZE_USEHEADER);
	}
	m_list->SetSelectionMark(0);
	m_list->SetRedraw(true);
	m_list->Invalidate();
}

int CDataTable::GetRecordCount()
{
    int nRows = 0;
    
    nRows = m_runsql.m_recordset->GetRecordCount();

    if(nRows == -1)
    {
        nRows = 0;
        m_runsql.m_recordset->MoveFirst();
        
        while(!m_runsql.m_recordset->adoEOF)
        {
            nRows++;
            m_runsql.m_recordset->MoveNext();
        }
        if(nRows > 0)
            m_runsql.m_recordset->MoveFirst();
    }
    
    return nRows;

}